﻿@using System.ComponentModel.DataAnnotations

<div class="demo-description" id="CustomForm">
    <h2><DemoNavLink Link="FormValidation#CustomForm" />Form Validation - Custom Form</h2>
    <p>
        The following example shows a standard <b>EditForm</b> component with a few DevExpress editors in it:
    </p>
    <ul>
        <li><a class="helplink" target="_blank" href="https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxTextBox">Text Box</a></li>
        <li><a class="helplink" target="_blank" href="https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxComboBox-2">ComboBox</a></li>
        <li><a class="helplink" target="_blank" href="https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxSpinEdit-1">Spin Edit</a></li>
        <li><a class="helplink" target="_blank" href="https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxDateEdit-1">Date Edit</a></li>
    </ul>
    <p>
        Once you attempt to submit changes, editors become marked with colored outlines.
        <span class="invalid" style="color:red;">Red</span> indicates invalid values, while
        <span class="modified" style="outline: 1px solid #26b050; color:#26b050;">green</span> indicates values that were posted successfully.
    </p>
    <p>
        The code also adds a standard <b>ValidationMessage</b> component for each editor to display error messages.
    </p>
</div>

<div class="card demo-card-wide demo-card-shadow">
    <div class="card-header">
        <DemoToolbar ShowSizeSelector="false"
                     Title="@FormValidationState" />
    </div>
    <div class="card-body">

        <EditForm Model="@starship" OnValidSubmit="@HandleValidSubmit" OnInvalidSubmit="@HandleInvalidSubmit">
            <DataAnnotationsValidator />
            <div class="row">
                <div class="col-md-6" style="padding-bottom: 12px">
                    <label for="identifier">Identifier: </label>
                    <DxTextBox Id="identifier" @bind-Text="@starship.Identifier" />
                    <ValidationMessage For="@(() => starship.Identifier)" />
                </div>
                <div class="col-md-6" style="padding-bottom: 12px">
                    <label for="classification">Primary Classification: </label>
                    <DxComboBox Id="classification" NullText="Select classification ..."
                                ClearButtonDisplayMode="DataEditorClearButtonDisplayMode.Auto"
                                Data="@(new List<Classification>() { new Classification(1, "Defense"), new Classification(2, "Exploration"), new Classification(3, "Diplomacy") })"
                                TextFieldName="Value"
                                ValueFieldName="Id"
                                @bind-Value="@starship.Classification" />
                    <ValidationMessage For="@(() => starship.Classification)" />
                </div>
                <div class="col-md-6" style="padding-bottom: 12px">
                    <label for="accommodation">Maximum Accommodation: </label>
                    <DxSpinEdit Id="accommodation" @bind-Value="@starship.MaximumAccommodation" />
                    <ValidationMessage For="@(() => starship.MaximumAccommodation)" />
                </div>
                <div class="col-md-6" style="padding-bottom: 12px">
                    <label for="productionDate">Production Date: </label>
                    <DxDateEdit @bind-Date="@starship.ProductionDate" />
                    <ValidationMessage For="@(() => starship.ProductionDate)" />
                </div>
                <div class="col-md-12">
                    <DxButton SubmitFormOnClick="true" Text="Submit" RenderStyle="ButtonRenderStyle.Secondary" />
                </div>
            </div>
        </EditForm>
    </div>
</div>

<CodeSnippet_FormValidation_Editors />

@code { string FormValidationState = @"Press the ""Submit"" button to validate the form.";
    private Starship starship = new Starship() { ProductionDate = DateTime.Now + TimeSpan.FromDays(1) };

    private void HandleValidSubmit() {
        FormValidationState = @"Form data is valid";
    }
    private void HandleInvalidSubmit() {
        FormValidationState = @"Form data is invalid";
    }

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
    public class DateInPastAttribute : ValidationAttribute {
        public override bool IsValid(object value) {
            return (DateTime)value <= DateTime.Today;
        }
    }

    public class Starship {
        [Required(ErrorMessage = "The Identifier value should be specified.")]
        [StringLength(16,
        ErrorMessage = "The Identifier exceeds 16 characters.")]
        public string Identifier { get; set; }

        [Range(1, 3, ErrorMessage = "The Primary Classification value should be specified.")]
        public int Classification { get; set; }

        [Range(1, 100000, ErrorMessage = "The Maximum Accommodation value should be a number between 1 and 100,000.")]
        public int MaximumAccommodation { get; set; }

        [Required]
        [DateInPastAttribute(ErrorMessage = "The Production Date value cannot be later than today.")]
        public DateTime ProductionDate { get; set; }
    }

    public class Classification {
        public int Id { get; set; }
        public string Value { get; set; }

        public Classification(int id, string value) {
            Id = id;
            Value = value;
        }

        public override int GetHashCode() {
            int result = 17;
            result = result * 31 + Value.GetHashCode();
            result = result * 31 + Id.GetHashCode();
            return result;
        }

        public override bool Equals(object obj) {
            var item = obj as Classification;
            if (item == null)
                return false;

            return item.Id == Id && item.Value == Value;
        }
    }
}
